package com.example.baseapp.controller;

import com.example.baseapp.common.ApiResponse;
import com.example.baseapp.entity.User;
import com.example.baseapp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    // 用户登录
    @PostMapping("/login")
    public ApiResponse<User> login(@RequestBody Map<String, Object> params) {
        String openid = params.get("openid").toString();
        String nickname = params.get("nickname") != null ? params.get("nickname").toString() : null;
        String avatar = params.get("avatar") != null ? params.get("avatar").toString() : null;
        Integer gender = params.get("gender") != null ? Integer.valueOf(params.get("gender").toString()) : null;
        return ApiResponse.success(userService.login(openid, nickname, avatar, gender));
    }
    
    // 更新用户信息
    @PostMapping("/update")
    public ApiResponse<User> updateUser(@RequestBody Map<String, Object> params) {
        Long userId = Long.valueOf(params.get("userId").toString());
        String nickname = params.get("nickname") != null ? params.get("nickname").toString() : null;
        String phone = params.get("phone") != null ? params.get("phone").toString() : null;
        String address = params.get("address") != null ? params.get("address").toString() : null;
        return ApiResponse.success(userService.updateUser(userId, nickname, phone, address));
    }
    
    // 获取用户信息
    @PostMapping("/info")
    public ApiResponse<User> getUserInfo(@RequestBody Map<String, Object> params) {
        Long userId = Long.valueOf(params.get("userId").toString());
        return ApiResponse.success(userService.getUserById(userId));
    }
} 